//
//  IntroductionViewController.m
//  WheatPlan
//
//  Created by 王晨辉 on 15/5/27.
//  Copyright (c) 2015年 IOSTeam. All rights reserved.
//

#import "IntroductionViewController.h"
#import "TeacherViewController.h"
#import "UIImageView+WebCache.h"
@interface IntroductionViewController ()

@end

@implementation IntroductionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [GetFileModel ShowProgressHUDwithMessage:@"耐心等待"];

    
    
    //设置导航栏头文字
    self.navigationItem.title = @"老师介绍";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#ef5858" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;
    
    [self subView];
    
    int a =  self.navigationController.navigationBar.frame.size.height+20;

    _IntroductionTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _IntroductionTableView.delegate = self;
    _IntroductionTableView.dataSource = self;
    _IntroductionTableView.rowHeight = (SCREEN_HEIGHT-a)/4;
    [MBProgressHUD showHUDAddedTo:_IntroductionTableView animated:YES];
    [self.view addSubview:_IntroductionTableView];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];
    
    NSDictionary *param = @{@"userId": ID,@"contentId":@"2"/*,@"ealyType":@"2"*/,@"babyId":[BabyManager shareInstance].babyId};
    
    
    
    
    [UserDAL getTeachersParams:param withBlock:^(NSArray *data, NSError *error) {
        
        
        
        
        ////NSLog(@"%@",data);
        
        imageArr = [NSMutableArray array];
        priceArr = [NSMutableArray array];
        nameArr  = [NSMutableArray array];
        typeArr  = [NSMutableArray array];
        identify  = [NSMutableArray array];
        type  = [NSMutableArray array];
        if (data.count!=0) {
            for (NSDictionary * dic in data) {
                
                
                
                NSString * imageUrlStr = [dic objectForKey:@"imageUrl"];
                NSString * priceStr = [dic objectForKey:@"price"];
                NSString * teacherNameStr = [dic objectForKey:@"teacherName"];
                NSString * typeStr = [dic objectForKey:@"breif"];
                NSString * identifyS = [dic objectForKey:@"identify"];
                NSString * type1 = [dic objectForKey:@"type"];
                
                
                [imageArr addObject:imageUrlStr];
                [priceArr addObject:priceStr];
                [nameArr addObject:teacherNameStr];
                [typeArr addObject:typeStr];
                [identify addObject:identifyS];
                [type addObject:type1];
                
                ////NSLog(@"%@,%@,%@,%@",imageUrlStr,priceStr,teacherNameStr,typeStr);
                
                ////NSLog(@"%d",nameArr.count);
                
                
            }
            
        }else{
            
            
            [MBProgressHUD hideHUDForView:_IntroductionTableView animated:YES];
            
            alert(@"对不起，你所在的地区没有麦苗公学。");
            
            
        }
        
        
        
        [_IntroductionTableView reloadData];
        
        
        
    }];

    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    
    
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}



-(void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


-(void)subView{
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
        
        return typeArr.count;
    
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    
    return 0;
}

#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    [MBProgressHUD hideHUDForView:_IntroductionTableView animated:YES];
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
    }
    int a =  self.navigationController.navigationBar.frame.size.height+20;
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"老师介绍线框@2x.png.png"]];
    imageView.frame = CGRectMake(5, 5, SCREEN_WIDTH-10, (SCREEN_HEIGHT-a)/4-10);
    [cell addSubview:imageView];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIImageView * teacherImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH/3.5, (SCREEN_HEIGHT-a)/4-20)];
    [teacherImageView sd_setImageWithURL:imageArr[indexPath.row] placeholderImage:nil];
    
    
    
    
    [imageView addSubview:teacherImageView];
    
    UILabel * teacherLabel = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, ((SCREEN_HEIGHT-a)/4-25)/2, SCREEN_WIDTH-20-SCREEN_WIDTH/3.7-10, ((SCREEN_HEIGHT-a)/4)/2)];
    teacherLabel.text = [NSString stringWithFormat:@"%@",typeArr[indexPath.row]];
    teacherLabel.font = [UIFont systemFontOfSize:12];
//            teacherLabel.backgroundColor = [UIColor redColor];
    teacherLabel.numberOfLines = 4;
    teacherLabel.textAlignment = NSTextAlignmentLeft;
    [imageView addSubview:teacherLabel];
    
    
    UILabel * teacherNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, 0, 125, 30)];
    teacherNameLabel.text = nameArr[indexPath.row];
    teacherNameLabel.font = [UIFont systemFontOfSize:20];
    //        teacherNameLabel.backgroundColor = [UIColor greenColor];
    teacherNameLabel.numberOfLines = 2;
    teacherNameLabel.textAlignment = NSTextAlignmentLeft;
    
    [imageView addSubview:teacherNameLabel];
    
    
    UILabel * teacherTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, teacherNameLabel.frame.origin.y+teacherNameLabel.frame.size.height, 125, 30)];
    teacherTypeLabel.text = [NSString stringWithFormat:@"%@",priceArr[indexPath.row]];
    teacherTypeLabel.font = [UIFont systemFontOfSize:12];
    teacherTypeLabel.numberOfLines = 2;
    teacherTypeLabel.textAlignment = NSTextAlignmentLeft;
//    [imageView addSubview:teacherTypeLabel];
    
    
    
    
    teacherTypeTLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-80, 0, 125, ((SCREEN_HEIGHT-a)/4-20)/2)];
    if ([type[indexPath.row] integerValue] == 1) {
        teacherTypeTLabel.text = @"早教老师";
    }else{
        teacherTypeTLabel.text = @"亲子老师";
    }
    teacherTypeTLabel.font = [UIFont systemFontOfSize:12];
    teacherTypeTLabel.numberOfLines = 2;
    teacherTypeTLabel.textColor = [UIColor blackColor];
    teacherTypeTLabel.textAlignment = NSTextAlignmentLeft;
    [imageView addSubview:teacherTypeTLabel];
    
    

    
            return cell;
    
    
    
    
    
}
#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
#pragma mark--- tableViewDelegat 点击方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TeacherViewController * tea = [[TeacherViewController alloc]init];
    tea.identify = identify[indexPath.row];
    tea.type = type[indexPath.row];
    tea.nameTea = nameArr[indexPath.row];
    tea.brief = typeArr[indexPath.row];
    tea.manei = priceArr[indexPath.row];
    tea.pic = imageArr[indexPath.row];

    [self.navigationController pushViewController:tea animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
